﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using log4net;
using log4net.Config;
using System.IO;
using WebLib.AuthorizeService;

namespace HinokyWeb.Controllers
{
    public class BaseController : Controller
    {
        public bool IsAjaxRequest { get; set; }

        public BaseController()
        {

        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Request.Headers["X-Requested-With"] != null
                && filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
            {
                IsAjaxRequest = true;
            }
            else
            {
                IsAjaxRequest = false;
            }

            base.OnActionExecuting(filterContext);
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            var logger = LogManager.GetLogger("WebLogger");
            LogicalThreadContext.Properties["UserId"] = MemberInfo.Instance.UserID;
            LogicalThreadContext.Properties["RefferURL"] = Request.UrlReferrer == null ? "/" : Request.UrlReferrer.OriginalString;

            logger.Error(filterContext.Exception.Message);

            base.OnException(filterContext);
        }
    }
}
